package com.maple.common.utils.agreements;
import com.maple.common.utils.Tools;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class B1TAgreementUtil {
    public static void testRegexp(String s,CallBackWeight callBackWeight) {
        if (s != null && !"".equals(s)) {
            String regex = "02[0-9a-fA-F]{20}03";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                String matcherString = matcher.group();
                System.out.println(matcherString);
                if (checkData(matcherString)) {
                    System.out.println("输出重量：" + readWeight(matcherString) + "kg");
//                    return readWeight(matcherString);



                    callBackWeight.succeed(String.valueOf(Double.parseDouble(readWeight(matcherString))*1000));
                }
            }
        }
    }
    /**
     * 读取重量
     * @param testData 16进制数字 字符串长度24
     * */
    public static String readWeight(String testData) {
//        double weight = 0.0;
//        for (int i = 0; i < 6; i++) {
//            String num = data.substring(2 * i + 4, 2 * i + 6);
//            int a = Integer.parseInt(num, 16) - 0x30;
//            int w = (int) Math.pow(10, 5 - i) * a;
//            weight += w;
//        }
//        BigDecimal decimal = new BigDecimal(weight);
//        String num = data.substring(2 * 8, 2 * 9);
//        int dian = Integer.parseInt(num, 16) - 0x30;//小数点位数
//        BigDecimal temp = new BigDecimal("0.1");
//        for (int i = 0; i < dian; i++) {
//            decimal = decimal.multiply(temp);
//        }
//        String sWeight = decimal.toString();
//        if (data.substring(2, 4).toUpperCase().equals("2D"))//符号位
//        {
//            sWeight = "-" + sWeight;
//        } else {
//            sWeight = "" + sWeight;
//        }
//        return sWeight;
        StringBuilder builder=new StringBuilder();
        byte[] data = Tools.parseHexStr2Byte(testData);
        for (int i = 1; i < 8; i++) {
            builder.append((char) data[i]);
        }
        int dian=Integer.parseInt((char)data[8]+"");
        if (dian<=1){
            builder.insert(builder.length()-dian,".");
        }
        BigDecimal decimal=new BigDecimal(builder.toString().trim());
        return decimal.toString();

    }


    /**
     * 检测数据正确性
     */
    public static boolean checkData(String testData) {
        byte[] data = Tools.parseHexStr2Byte(testData);
        if (testData.length() == 24) {
            if (Integer.parseInt(testData.substring(0, 2), 16) != 0x02) {
                System.out.println("开始位错误");
                return false;
            }
            if (Integer.parseInt(testData.substring(2 * 11, 2 * 12), 16) != 0x03) {
                System.out.println("结束位错误");
                return false;
            }
            byte[] a = Arrays.copyOfRange(data, 1, 9);
            String res = Tools.checkXor(Tools.parseByte2HexStr(a));

            if (res.length() == 2) {
                String dataH = Integer.toHexString(data[9] & 0xFF);
                if (dataH.length() == 1) {
                    dataH = '0' + dataH.toUpperCase();
                }
                String checkH = getCheckHex(res.charAt(0) + "");
                if (dataH.equals(checkH)) {
                    System.out.println("高位校验成功");
                } else {
                    return false;
                }

                String dataL = Integer.toHexString(data[10] & 0xFF);
                if (dataL.length() == 1) {
                    dataL = '0' + dataL.toUpperCase();
                }
                String checkL = getCheckHex(res.charAt(1) + "");
                if (dataL.equals(checkL)) {
                    System.out.println("低位校验成功");
                    return true;
                } else {
                    return false;
                }

            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    /***
     * 校验计算
     * 高位或低位小于等于9  +30H
     * 大于9  +37H
     * **/
    public static String getCheckHex(String s) {
        int h = Integer.parseInt(s + "", 16);//异或校验高位
        if (h <= 9) {
            return Tools.integerToHexString(h + 0x30);
        } else {
            return Tools.integerToHexString(h + 0x37);
        }
    }

}
